// packageContract/pages/contract-details/contract-details.js
import {contractTemplateDetails} from '../../api/contract'
import { toast } from '../../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        templateId:'',
        list:[],
        total:'',
        unsigned:'',
        input:'',
        pageNum:1,
        pageSize:20,
        hasNextPage:true,
        queryFlag:'',
        eject: false,
        relaunchId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options,'options')
        
        this.setData({
            templateId:options.templateId,
            total:options.total,
            unsigned:options.unsigned,
            queryFlag:options.type
        })
        this.contractTemplate()
    },
    contractTemplate(searchText=''){
        contractTemplateDetails({
            // templateId:this.data.templateId,
            searchText:searchText,
            pageNum:this.data.pageNum,
            pageSize:this.data.pageSize,
            queryFlag:this.data.queryFlag
        }).then(res=>{
            console.log(res,'res')
            res.list.forEach((item,index)=>{
                res.list[index].sexName = item.sex === '0' ? '男' : '女'
            })
            if(!res.hasNextPage){
                this.setData({
                    hasNextPage:false
                })
            }
            const add = []
            if(!searchText){
                add.push(...this.data.list)
                add.push(...res.list)
                this.setData({
                    list:add,
                    total:res.totalCount,
                    unsigned:res.unsignedCount
                })
                return
            }
            this.setData({
                list:res.list,
                total:res.totalCount,
                unsigned:res.unsignedCount
            })
            
        })
    },
    // 搜索
    searchWorker(event){
        const { value } = event.detail
        this.setData({
            input:value
        })
        if(!this.data.input){
            this.setData({
                pageNum:1,
                hasNextPage:true,
                list:[]
            })
        }
        this.contractTemplate(this.data.input)
    },
    scrolltolower(){
        console.log('到底部啦')
        if(!this.data.hasNextPage){
            return
        }
        this.setData({
            pageNum:this.data.pageNum+=1
        })
        console.log('执行啦吗')
        this.contractTemplate()
    },
    // 出现弹框 取消弹框
    onRelaunchEleSign(event){
        const { id } = event.currentTarget.dataset
        this.setData({
            eject:!this.data.eject,
            relaunchId:id
        })
            
    },
    onfailContractComplete(){
        this.setData({
            list:[],
            pageNum:1,
            nextPage:true,
            eject:!this.data.eject,
            input:''
        })
        toast('操作成功')
        setTimeout(()=>{
            this.contractTemplate()
        },1500)
    }
})